package zero.tech.games.jh.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import zero.tech.core.data.Resp;
import zero.tech.games.common.Code;
import zero.tech.games.jh.data.JhConfig;
import zero.tech.games.jh.data.JhGameBase;
import zero.tech.games.jh.data.JhRoomCache;
import zero.tech.games.jh.data.JhRoomStatus;
import zero.tech.games.jh.data.JhSmallPanel;
import zero.tech.games.jh.data.JhTimer;
import zero.tech.games.jh.data.consts.JHCMD;
import zero.tech.games.jh.data.entity.Card;
import zero.tech.games.jh.data.entity.JhOut;
import zero.tech.games.jh.data.entity.PlayerData;
import zero.tech.games.jh.data.notify.NotifyGameStart;
import zero.tech.games.jh.service.api.IJhService;
import zero.tech.games.jh.util.CardUtil;
import zero.tech.games.jh.util.PackageUtil;
import zero.tech.games.jh.util.RoomUtil;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.wsnet.controller.LogicController;
import zero.tech.wsnet.data.Message;

@Service
public class JhServiceImpl implements IJhService{

	@Override
	public ResReconnet roomReconnect(Room room, LogicController online) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void roomTimer(Room room) {
		long now=System.currentTimeMillis();
		int room_status=room.getRoom_status();
		JhTimer jhTimer = (JhTimer) room.getTimer();
		JhConfig jhConfig = (JhConfig) room.getGameConfig();
		if(room_status==JhRoomStatus.DEFAULT) {
			RoomUtil.checkGameStart(room);
		}else if(room_status==JhRoomStatus.WAIT_NEXT_ROUND){
			RoomUtil.checkGameStart(room);
		}else if(room_status==JhRoomStatus.CALCULATING) {
			
		}		
	}

	@Override
	public void gameStart(Room room) {
		initRoomBasic(room);
		initHandCards(room);
		initConfirmBanker(room);
		initSmallPanel(room);
		notifyGameStart(room);			
	}


	private void notifyGameStart(Room room) {
		for(LogicController logicController:room.getControllersWithoutWatcherAndTaotai().values()) {
			NotifyGameStart notifyGameStart=PackageUtil.packageNotifyGameStart(room);
			logicController.getWebClient().sendMessage(Message.build(JHCMD.NOTIFY_GAME_START, Resp.build(Code.OK, "通知游戏开始",notifyGameStart)));
		}
		for(LogicController logicController:room.getWatcherAndTaotai().values()) {
			NotifyGameStart notifyGameStart=PackageUtil.packageNotifyGameStart(room);
			logicController.getWebClient().sendMessage(Message.build(JHCMD.NOTIFY_GAME_START, Resp.build(Code.OK, "通知游戏开始",notifyGameStart)));
		}
	}

	private void initSmallPanel(Room room) {
		JhRoomCache jhRoomCache = (JhRoomCache) room.getRoomCache();
		int room_times=room.getRoom_times();
		String room_id=room.getRoom_id();
		String configStr=room.getConfigStr();
		JhSmallPanel jhSmallPanel = new JhSmallPanel();
		jhSmallPanel.setRoom_id(room_id);
		jhSmallPanel.setConfigStr(configStr);		
		jhRoomCache.getSmallPanels().put(room_times, jhSmallPanel);			
	}

	private void initConfirmBanker(Room room) {
		JhGameBase jhGameBase =(JhGameBase) room.getGameBase();
		List<Integer> uids=new ArrayList<>();
		uids.addAll(room.getControllersWithoutWatcherAndTaotai().keySet());
		Collections.shuffle(uids);
		int banker_uid=uids.get(0);
		jhGameBase.setBanker_uid(banker_uid);
		int dispatch_uid=LogicUtil.getNextPosByUid(room,banker_uid);
		jhGameBase.setDispatch_uid(dispatch_uid);
	}

	private void initHandCards(Room room) {
		JhGameBase jhGameBase =(JhGameBase) room.getGameBase();
		Map<Integer, LogicController> controllers=room.getControllersWithoutWatcherAndTaotai();
		int size=controllers.size();
		List<List<Card>> tmps=new ArrayList<>();
		List<Card> deck=new ArrayList<>();
		deck.addAll(CardUtil.deckWithoutJoker());
		Collections.shuffle(deck);
		for(int i=0;i<size;i++) {
			List<Card> hands=new ArrayList<Card>();
			hands.addAll(deck.subList(i*3, i*3+3));
			tmps.add(hands);
		}
		int index=0;
		for(LogicController controller:controllers.values()) {
			int uid=controller.getId();
			List<Card> hands=tmps.get(index++);
			JhOut jhOut=CardUtil.calJhOut(hands);
			PlayerData playerData=new PlayerData(uid,jhOut);
			jhGameBase.getPlayers().put(uid,playerData);
			jhGameBase.getHandsmap().put(uid, hands);//战绩使用
		}		
	}

	private void initRoomBasic(Room room) {
		JhTimer jhTimer=(JhTimer) room.getTimer();
		room.setRoom_status(JhRoomStatus.GAMING);
		room.setRoom_times(room.getRoom_times()+1);
		jhTimer.lastBeginDispatchTimer=System.currentTimeMillis();		
	}

}
